﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;
using System.Web;

namespace Twipler.Xtensions
{
    public static class StringHelper
    {
        public static string FullUrlFromVirtualPath(this string virtualPath)
        {
            if (virtualPath == null)
                return null;

            if (virtualPath.StartsWith("~"))

                return (HttpContext.Current.Request.ApplicationPath +
                        virtualPath.Substring(1)).Replace("//", "/");

            return virtualPath; 
        }

		public static string Replace(this string original, string find, string replaceWith, StringComparison comparison)
		{
			int index = original.IndexOf(find, comparison);
			if (index == -1)
				return original;

			return original.Substring(0, index) + replaceWith + original.Substring(index + find.Length);
		}

        public static bool ContainsOnly(this string text, string whiteList)
        {
            foreach (Char c in text)
                if (whiteList.IndexOf(c) == -1)
                    return false;

            return true;
        }

		public static List<T> AsJsonList<T>(this string jsonInts, Func<string, T> parse)
        {
			int index = 1;
            int nextIndex = 0;

            List<T> values = new List<T>();

            if (jsonInts.Length < 2)
                return values;

            while (nextIndex + 2 < jsonInts.Length)
            {
                nextIndex = jsonInts.IndexOf(',', index);
                if (nextIndex == -1)
                    nextIndex = jsonInts.Length - 2;

				values.Add(parse(jsonInts.Substring(index, nextIndex - index)));

                index = nextIndex + 1;
            }

            return values;
        }

       
        /// <summary>
        /// Compute the distance between two strings.
        /// </summary>
        public static int LevenshteinDistance(this string s, string t)
        {
            /* 
             *  Taken from Sam Allen's website http://dotnetperls.com/levenshtein
             */
  
            int n = s.Length;
            int m = t.Length;
            int[,] d = new int[n + 1, m + 1];

            // Step 1
            if (n == 0)
            {
                return m;
            }

            if (m == 0)
            {
                return n;
            }

            // Step 2
            for (int i = 0; i <= n; d[i, 0] = i++)
            {
            }

            for (int j = 0; j <= m; d[0, j] = j++)
            {
            }

            // Step 3
            for (int i = 1; i <= n; i++)
            {
                //Step 4
                for (int j = 1; j <= m; j++)
                {
                    // Step 5
                    int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

                    // Step 6
                    d[i, j] = Math.Min(
                        Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                        d[i - 1, j - 1] + cost);
                }
            }
            // Step 7
            return d[n, m];
        }
    }
}